﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MKDiractorySearch
{
    /// <summary>
    /// FileTypes
    /// </summary>
    public enum FileType
    {
        Audio,
        Video,
        Text,
        Binary,
        Executable,
        NotDefined
    }

    /// <summary>
    /// MKFile info
    /// </summary>
    public class MKFile
    {
        /// <summary>
        /// Gets or sets the name of the file.
        /// </summary>
        /// <value>
        /// The name of the file.
        /// </value>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the file path.
        /// </summary>
        /// <value>
        /// The file path.
        /// </value>
        public string Path { get; set; }

        /// <summary>
        /// Gets or sets the size.
        /// </summary>
        /// <value>
        /// The size.
        /// </value>
        public long Size { get; set; }

        /// <summary>
        /// Gets or sets the extension.
        /// </summary>
        /// <value>
        /// The extension.
        /// </value>
        public string Extension { get; set; }

        /// <summary>
        /// Gets or sets the type of the file.
        /// </summary>
        /// <value>
        /// The type of the file.
        /// </value>
        public FileType Type { get; private set; }

        /// <summary>
        /// Prevents a default instance of the <see cref="MKFile"/> class from being created.
        /// </summary>
        private MKFile()
        {

        }

        /// <summary>
        /// Reads the file.
        /// </summary>
        /// <param name="filepath">The filepath.</param>
        /// <returns></returns>
        public static MKFile Read(string filepath)
        {
            FileInfo file = new FileInfo(filepath);
            return Read(fileInfo: file);
        }

        /// <summary>
        /// Reads the file.
        /// </summary>
        /// <param name="fileInfo">The file info.</param>
        /// <returns></returns>
        public static MKFile Read(FileInfo fileInfo)
        {
            var mkfile = new MKFile();
            mkfile.Name = fileInfo.Name;
            mkfile.Path = fileInfo.FullName;
            mkfile.Size = fileInfo.Length;
            mkfile.Extension = fileInfo.Extension;
            mkfile.Type = mkfile.GetFileType();
            return mkfile;
        }

        private FileType GetFileType()
        {
            IDictionary<string, IEnumerable<string>> availableFileTypes =
                new Dictionary<string, IEnumerable<string>>();
            availableFileTypes.Add("Audio", new List<string> { "mp3", "aac", "wma", "ogg" });
            availableFileTypes.Add("Video", new List<string> { "avi", "mkv", "mp4", "rm", "wmv" });
            availableFileTypes.Add("Text", new List<string> { "txt", "cs", "doc", "ini" });
            availableFileTypes.Add("Binary", new List<string> { "exe", "bin", "msi" });

            var matchedType = availableFileTypes.FirstOrDefault(k => k.Value.Contains(this.Extension.Trim('.')));
            if (!matchedType.Equals(null) && matchedType.Key != null)
                return (FileType)Enum.Parse(typeof(FileType), matchedType.Key);
            return FileType.NotDefined;
        }
    }
}
